package com.example.blogback.controller;

import com.example.blogback.dto.ApiResponse;
import com.example.blogback.dto.CategoryDTO;
import com.example.blogback.entity.Category;
import com.example.blogback.service.CategoryService;
import com.example.blogback.vo.CategoryTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("创建分类")
    @PostMapping
    public ApiResponse<Void> createCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.createCategory(categoryDTO);
        return ApiResponse.success();
    }

    @ApiOperation("更新分类")
    @PutMapping
    public ApiResponse<Void> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return ApiResponse.success();
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return ApiResponse.success();
    }

    @ApiOperation("获取分类树结构")
    @GetMapping("/tree")
    public ApiResponse<List<Category>> getCategoryTree() {
        return ApiResponse.success(categoryService.getCategoryTree());
    }

    @ApiOperation("获取顶级分类列表")
    @GetMapping("/top")
    public ApiResponse<List<Category>> getTopCategories() {
        return ApiResponse.success(categoryService.getTopCategories());
    }

    @ApiOperation("获取指定分类的子分类列表")
    @GetMapping("/sub/{parentId}")
    public ApiResponse<List<Category>> getSubCategories(@PathVariable Long parentId) {
        return ApiResponse.success(categoryService.getSubCategories(parentId));
    }

    @ApiOperation("获取指定分类及其文章的树形结构")
    @GetMapping("/tree/{categoryId}/articles")
    public ApiResponse<CategoryTreeVO> getCategoryTreeWithArticles(@PathVariable Long categoryId) {
        return ApiResponse.success(categoryService.getCategoryTreeWithArticles(categoryId));
    }
}